using System.Linq;
using System.Web.Mvc;
using OurWedding1.Models;

namespace OurWedding1.Controllers
{
    public class MustHaveSongsController : BaseController
    {
        private readonly IWeddingRepository _weddingRepository;
        private readonly IMustHaveSongRepository _musthavesongRepository;

        // If you are using Dependency Injection, you can delete the following constructor
        public MustHaveSongsController()
            : this(new WeddingRepository(), new MustHaveSongRepository())
        {
        }

        public MustHaveSongsController(IWeddingRepository weddingRepository, IMustHaveSongRepository musthavesongRepository)
        {
            _weddingRepository = weddingRepository;
            _musthavesongRepository = musthavesongRepository;
        }

        //
        // GET: /MustHaveSongs/

        public ActionResult Index()
        {
            var selectedWedding = SelectedWeddingId;
            if (!selectedWedding.HasValue)
            {
                return RedirectToAction("Index", "Weddings");
            }
            var model =
                _musthavesongRepository.
                    AllIncluding(musthavesong => musthavesong.Wedding)
                                      .Where(d => d.WeddingId == selectedWedding.Value)
                                      .OrderBy(d => d.Artist);

            return View(model);
        }

        //
        // GET: /MustHaveSongs/Details/5

        public ViewResult Details(int id)
        {
            return View(_musthavesongRepository.Find(id));
        }

        //
        // GET: /MustHaveSongs/Create

        public ActionResult Create()
        {
            var selectedWedding = SelectedWeddingId;
            if (!selectedWedding.HasValue)
            {
                return RedirectToAction("Index", "Weddings");
            }
            var model = new MustHaveSong { WeddingId = selectedWedding.Value };
            return View(model);
        }

        //
        // POST: /MustHaveSongs/Create

        [HttpPost]
        public ActionResult Create(MustHaveSong musthavesong)
        {
            if (ModelState.IsValid)
            {
                _musthavesongRepository.InsertOrUpdate(musthavesong);
                _musthavesongRepository.Save();
                return RedirectToAction("Index");
            }
            ViewBag.PossibleWeddings = _weddingRepository.All;
            return View();
        }

        //
        // GET: /MustHaveSongs/Edit/5

        public ActionResult Edit(int id)
        {
            ViewBag.PossibleWeddings = _weddingRepository.All;
            return View(_musthavesongRepository.Find(id));
        }

        //
        // POST: /MustHaveSongs/Edit/5

        [HttpPost]
        public ActionResult Edit(MustHaveSong musthavesong)
        {
            if (ModelState.IsValid)
            {
                _musthavesongRepository.InsertOrUpdate(musthavesong);
                _musthavesongRepository.Save();
                return RedirectToAction("Index");
            }
            ViewBag.PossibleWeddings = _weddingRepository.All;
            return View();
        }

        //
        // GET: /MustHaveSongs/Delete/5

        public ActionResult Delete(int id)
        {
            return View(_musthavesongRepository.Find(id));
        }

        //
        // POST: /MustHaveSongs/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            _musthavesongRepository.Delete(id);
            _musthavesongRepository.Save();

            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                _weddingRepository.Dispose();
                _musthavesongRepository.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

